package com.sql.controller;

import com.sql.model.BookInfo;
import com.sql.service.BookInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;

/**
 * Created by 走过全世界。 on 2017/5/31 0031.
 */
@Controller
public class BookInfoController {

    private static final Logger logger = LoggerFactory.getLogger(BookInfoController.class);
    @Autowired
    private BookInfoService bookInfoService;


    //get方式提交的参数编码，只支持iso8859-1编码。所以改成post的请求方式
    @RequestMapping(value = "/query")
    public String queryBook(@RequestParam(value = "bookName",
            required = false, defaultValue = "") String bookName,
                            HttpServletRequest request) {
        ArrayList<BookInfo> bookInfos = bookInfoService.selectByBookName(bookName);
        logger.info("---------------->searching:" + bookName);
        if (!bookInfos.isEmpty()) {
            logger.info("---------------->query");
            request.setAttribute("books", bookInfos);
        }else {
            request.setAttribute("message","当前查询图书不存在，请重新输入");
        }
        return "query";
    }

    @RequestMapping(value = "/queryForm")
    public String queryForm() {
        logger.info("to queryForm");
        return "query";
    }

}
